$(function () {
    init_cusSale();
    $("body").on('click','[data-stopPropagation]',function (e) {e.stopPropagation();});
    /**用户搜索**/
    $('#bpdirect').live('input propertychange', function() {
        $JuXue.refreshTable("onSchoolTab");
    });

    $("#advance-daterange span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));

    $('#advance-daterange').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#advance-daterange span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("onSchoolTab");
    });
})

/**加载客户资料*/
function viewBigEmpDetail(btn) {
    var ids = $(btn).attr("data-id");
    if (ids == undefined){$JuXue.n_danger("获取资料失败");return;}
    $("#customer_ids").val(ids);
    $.post(ctx+"seaOnschool/loadCustomerDetail",{},function (r) {
        $("#myCustomer-modal").empty().append(r).modal('show').draggable();
    })
}

/**初始化客户阶段**/
function init_cusSale(){
    var settings = {
        url: ctx + "seaOnschool/list",
        pageSize: 15,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                osNickname:$("#bpdirect").val(),
                starttime :$("#starttime").val(),     //考试时间
                endtime :$("#endtime").val()     //结束时间
            };
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'osNickname',
                title: '昵称',
                width: 350,
                clickToSelect:false,
                class:'text-center',
                formatter:function (value, row, index) {
                    return "<a href='javascript:void(0)' onclick='viewBigEmpDetail(this)' data-id='"+row.osId+"'>"+value+"</a>";
                }
            }, {
                field: 'osMobile',
                class:'text-center',
                title: '账号'
            }, {
                field: 'osMyCustomerName',
                class:'text-center',
                title: '邀请人'
            }, {
                field: 'osMyCustomer',
                class:'text-center',
                title: '用户状态',
                formatter:function (value, row, index) {
                    if(value==null||value==""){
                        return "未分配";
                    }else{
                        return "已分配";
                    }
                }
            }, {
                field: 'osCreateTime',
                class:'text-center',
                title: '注册时间',
                formatter:function (value, row, index) {
                    return value.substring(0,10);
                }
            }
        ],
        onClickCell:function(field, value, row, $element){
            if(field == "osNickname"){
                return ;
            }else{
                var a = ".theme-panel", b = "active";
                //$(a).hasClass(b) ? $(a).removeClass(b) : $(a).addClass(b)
                $(a).addClass(b);
                $("#selectTheme").show();
                var selected = $("#onSchoolTab").bootstrapTable('getSelections');
                var selected_length = selected.length;
                $("#sutomeSelect").text(selected_length+1);
            }
        },
        onUncheck:function (row,ele) {
            var selected = $("#onSchoolTab").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                var a = ".theme-panel", b = "active";
                $(a).removeClass(b);
                $("#selectTheme").hide();
            }
            $("#sutomeSelect").text(selected_length+1);
        },
        onCheck:function (row,ele) {
            var selected = $("#onSchoolTab").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                var a = ".theme-panel", b = "active";
                $(a).removeClass(b);
                $("#selectTheme").hide();
            }else{
                var a = ".theme-panel", b = "active";
                $(a).addClass(b);
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onUncheck:function (rows) {
            var selected = $("#onSchoolTab").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                var a = ".theme-panel", b = "active";
                $(a).removeClass(b);
                $("#selectTheme").hide();
            }else{
                var a = ".theme-panel", b = "active";
                $(a).addClass(b);
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onCheckAll:function (rows) {
            var a = ".theme-panel", b = "active";
            //$(a).hasClass(b) ? $(a).removeClass(b) : $(a).addClass(b)
            $(a).addClass(b);
            $("#selectTheme").show();
            $("#sutomeSelect").text(rows.length);
        },
        onUncheckAll:function (rows) {
            var a = ".theme-panel", b = "active";
            $(a).removeClass(b);
            $("#selectTheme").hide();
        }
    };
    $JuXue.initTable('onSchoolTab', settings);
}


/**导出客户资料**/
function exportCustomerInfo() {
    var ids = $("#onSchoolTab").bootstrapTable("getSelections");
    var ids_arr = "";
    if (!ids.length) {$JuXue.n_warning("请勾选需要导出的客户信息！");return;}
    for (var i = 0; i < ids.length; i++) {
        ids_arr += ids[i].bpId;
        if (i !== (ids.length - 1)) ids_arr += ",";
    }
    $.post(ctx+"seaOnschool/exportOnSchoolData",{"keyids":ids_arr},function(r){
        if (r.code === 0) {
            window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        } else {
            $JuXue.n_warning(r.msg);
        }
    });
}
